<template>
    <div class="marginR10 marginB10">
        <div style="width: 100%">
          <el-upload
                  :disabled="disabled"
                  class="avatar-uploader"
                  :action="actionUrl"
                  :show-file-list="false"
                   name="file"
                   :limit="num"
                  :multiple="num&&num>1?true:false"
                  accept=".jpg,.png,.jpeg,.gif"
                  :on-success="handleAvatarSuccessCover"
                  :before-upload="beforeAvatarUpload">
            <img v-if="ruleForm.url" :src="ruleForm.url" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </div>
      </div>
</template>

<script>
    export default {
      props:[
        'disabled','limit'
      ],
        data(){
            return{
                 num:this.limit?this.limit:1,
                actionUrl:'',
                ruleForm:{
                    url:'',
                }
            }
        },
  
        created(){
        },
        methods: {
           
            handleAvatarSuccessCover(res, file){//头像
                    let that = this;
                    if(res.code===0){
                       that.ruleForm.url =  res.data.path;
                       that.$emit('callbackUrl',that.ruleForm.url )
                    }else{
                     that.$ErrorToast(res);
                    }
                },
                beforeAvatarUpload(file) {
                    this.isEditDisabled = false;
                    const isLt2M = file.size / 1024 / 1024 < 2;

                    if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                    }
                    return  isLt2M;
                },
        },
    }
</script>
<style scoped lang="scss">
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 160px;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 160px;
    height: 160px;
    line-height: 160px;
    text-align: center;
  }
  .avatar {
    width: 160px;
    height: 160px;
    display: block;
  }
  ::v-deep.el-upload{
    width: 160px;
    height: 160px;
  }
  ::v-deep .el-upload--text{
    width: 160px !important;  
    height: 160px;
  }
  ::v-deep .el-dialog__body{
    padding: 0;
  }
</style>
